function toggle( id ) {
  var node = document.getElementById( id );
  var on = node.style.display != 'block';
  if( on )
    node.style.display = 'block';
  else
    node.style.display = 'none';
}

var noter = {};

function trim( s ) {
  return (s||'').replace( /^\s+|\s+$/g, '' );
}

function copy( o ) {
  var x = {};
  for( var i in o )
    x[i] = o[i];
  return x;
}

function format_date( d ) {
  function p(n) { return n<10?'0'+n:n }
  return [d.getFullYear(), p(d.getMonth()+1), p(d.getDate())].join('-');
}

function render( not, d ) {
  var id = not["löpnr"];
  var namn = not.titel, av = not["tonsättare"], st = not["besättning"];
  var info = not.kommentar, format = not.format;
  if( namn ) {
    namn = '"'+ namn +'"';
    if( av )
      namn += " av " + av;
  } else if( av )
    namn = av;
  else
    namn = '<i>(namnuppgift saknas i notfiskalens kalkylark)</i>';
  if( id )
    namn = id.bold() + ": " + namn;
  if( not.kommentar )
    namn += ' -- '+ not.kommentar;
  if( not.format )
    namn += ', '+ not.format +'-format.';
  return '<li><i class="datum">'+ format_date( d ) +' - </i>'+ namn +'</li>';
}

function parse_date_rounding_down( input, now ) {
  var date = new Date( now.getFullYear(), 0, 1, 0, 0, 0 );
  if( !(input = trim( input )) ) return date;
  var x = input.split('-');
  if( !x[0] ) return date;
  date.setFullYear( parseInt( x[0], 10 ) );
  if( !x[1] ) return date;
  date.setMonth( parseInt( x[1], 10 ) - 1 );
  if( !x[2] ) return date;
  date.setDate( parseInt( x[2], 10 ) );
  return date;
}

function parse_date_rounding_up( input, now ) {
  var y, Y = now.getFullYear(), m, M = now.getMonth(), D = now.getDate();
  var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  var date = new Date( Y, 11, 31, 23, 59, 59 );
  if( !(input = trim( input )) ) return date;
  var x = input.split('-');
  if( !x[0] ) return date;
  date.setFullYear( y = parseInt( x[0], 10 ) );
  if( (y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0)) )
    days[1] = 29;
  if( !x[1] ) return date;
  date.setDate( 1 ); // overflow precaution
  date.setMonth( m = parseInt( x[1], 10 ) - 1 );
  date.setDate( days[m] );
  if( !x[2] ) return date;
  date.setDate( parseInt( x[2], 10 ) );
  return date;
}

function parse_later_date( input, now ) {
  var Y = now.getFullYear(), M = now.getMonth(), D = now.getDate();
  var date = new Date( Y, 0, 1, 0, 0, 0 );
  if( !(input = trim( input )) ) return date;
  var x = input.split('-');
  if( !x[0] ) return date;
  date.setFullYear( y = parseInt( x[0], 10 ) );
  if( !x[1] ) return date;
  date.setMonth( m = parseInt( x[1], 10 ) - 1 );
  if( !x[2] ) return date;
  date.setDate( d = parseInt( x[2], 10 ) );
  return date;
}

function date_descending( a, b ) {
  var A = parseInt( a["löpnr"]||'0', 36 );
  var B = parseInt( b["löpnr"]||'0', 36 );
  a = a && a.date && a.date.valueOf() || 0;
  b = b && b.date && b.date.valueOf() || 0;
  return b - a || A - B;
}

function editgridCallback( json ) {
  var now = new Date, all = { out:[], back:[] };
  var props = ["tonsättare", "titel", "kommentar", "format"];
  var html = { out:'', back:'' }, not;
  for( var i = 0; not = json.items[i]; i++ ) {
    if( not.type == "Noter" ) {
      var id = trim( not["löpnr"]||'' );
      if( id )
 noter[id] = not;
      continue;
    }
    if( not.type != "Ut/In" )
      continue;
    id = trim( not["löpnr"]||'' );
    var out = parse_date_rounding_down( not["ut"], now );
    var back = parse_date_rounding_up( not["in"], now );
    var info = copy( noter[id] ) || {}, prop;
    for( var j = 0; prop = props[j]; j++ )
      if( not[prop] )
 info[prop] = not[prop];
    info["löpnr"] = not["löpnr"];
    if( out < now )
      if( back > now ) {
 info.date = out;
 info.html = render( info, out )
 all.out.push( info ); //html.out += render( info, out );
      } else {
 info.date = back;
 info.html = render( info, back );
 all.back.push( info );//html.back += render( info, back );
      }
  }
  all.out.sort( date_descending );
  all.back.sort( date_descending );
  for( var i = 0; not = all.out[i]; i++ ) html.out += not.html;
  for( var i = 0; not = all.back[i]; i++ ) html.back += not.html;
  var visa = function() {
    document.getElementById( 'ute' ).innerHTML = html.out;
    document.getElementById( 'tillbaka' ).innerHTML = html.back;
  };
  setTimeout( visa, 1e3 );
}

function toggle_dates() {
  var dates = document.getElementsByTagName( 'i' ), node;
  for( var i = 0; node = dates[i]; i++ ) {
    if( node.className != 'datum' ) continue;
    node.style.display = node.style.display == 'none' ? 'inline' : 'none';
  }
}

document.write("<a href='javascript:toggle_dates()'>show/hide dates</a><br />");
document.write("<a href=\"javascript:toggle('ute')\">Music in circulation:</a><br /><ul id='ute' style='display:block;'><li>(loading music info...)</li></ul>");
document.write("<a href=\"javascript:toggle('tillbaka')\">Music we should hand in:</a><br /><ul id='tillbaka' style='display:none;'><li>(loading music info...)</li></ul>");

var url = 'http://www.editgrid.com/user/tomaskoren.web/' +
  'tomas-noter.null.exhibit.jsonp?' + ((new Date).valueOf());
document.write( '<scr'+'ipt type="text/javascript" charset="UTF-8" src="' +
                url +'"></scr'+'ipt>' );
